<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Negocios_Model
 *
 * @author marcelo.diaz
 */
class Negocios_Model extends Model{
  function Negocios_Model(){
    parent::Model(4);
  }

  function _all(){     
  	 $query = $this->db->query("SELECT * 
  	 							FROM marker
  	 							WHERE marker.marker_cat = $this->category AND
  	 								 aproved = 1
  	 							ORDER BY name
  	 							");
	 $places = $query->result_array();
	 foreach ($places as $k => $data){
	 	//we MUST detect if icon and image are local, and add them the correct absolute path
	 	$places[$k]['icon'] = base_url()."images/icons/".$data['icon'];
	 	if(strpos($data['img'], ':') === false){
	 		$places[$k]['img'] = $places[$k]['icon'];
	 	}
	 }	 	 
     return $places;
  }
  
  /**
   * 
   * @param String $place: name of the local bussines to search
   */
  function get_place($place){
  	$place = mysql_real_escape_string($place);
  	$related = explode('-', $place);
  	$query = $this->db->query("SELECT *
					  			FROM marker
					  			WHERE marker.marker_cat = $this->category AND
  									 aproved = 1 AND
  									  marker.name like '$place'
				  			");
  	//$row = $query->row();
  	$row = $query->row_array();
  	if(empty($row)){
  		return false;
  	}else{
	  	//we MUST detect if icon and image are local, and add them the correct absolute path
	  	$row['icon'] = base_url()."images/icons/".$row['icon'];
	  	if(strpos($row['img'], ':') === false){
	  		$row['img'] = $row['icon'];
	  	}  	
	  	//get Related items
	  	foreach ($related as $k=>$w){
	  		$related[$k] = "marker.name like '%".$w."%'";
	  	}
	  	$where = implode(" OR ", $related);
	  	$query = $this->db->query("SELECT *
	  			FROM marker
	  			WHERE ($where) AND
	  					aproved = 1 AND
	  					(marker.name NOT LIKE '$place')
	  			ORDER BY marker.name
	  			");
	  			$relates = $query->result_array();
	  			//we MUST detect if icon and image are local, and add them the correct absolute path
	  			foreach ($relates as $k => $v){
	  			$relates[$k]['icon'] = base_url()."images/icons/".$v['icon'];
	  			if(strpos($v['img'], ':') === false){
	  			$relates[$k]['img'] = $relates[$k]['icon'];
	  			}
	  			}
	  			$row['related'] = $relates;	  	
	  	return $row;  	
  	}
  }   
}
?>
